package com.zrf.testchatgpt.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrf.testchatgpt.entity.Detail;
import com.zrf.testchatgpt.entity.Users;
import com.zrf.testchatgpt.entity.vo.DetailVo;
import com.zrf.testchatgpt.entity.vo.UserVo;
import com.zrf.testchatgpt.mapper.DetailMapper;
import com.zrf.testchatgpt.util.Result;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zrf
 * @since 2023-03-15
 */
@RestController
@RequestMapping("/detail")
@RequiresRoles(value = {"user","admin"},logical= Logical.OR)
public class DetailController {
    @Autowired
    DetailMapper detailMapper;
   @GetMapping("getDetailById")
    public Result getDetailById(String userId){
    LambdaQueryWrapper<Detail> wrapper=new LambdaQueryWrapper<>();
    wrapper.eq(Detail::getUserId,userId);
    List<Detail> details = detailMapper.selectList(wrapper);
    if(details!=null){
        return new Result("200","查询成功",details);
    }
    return new Result("500","查询失败",null);
    }
   @PostMapping("insert")
    public Result insertDetail(@RequestBody Detail detail){
        if (detail==null){
            return Result.builder().code("500").msg("参数为空").data(null).build();
        }
        detail.setIsDelete("1");
       int i = detailMapper.insertDetail(detail);
        if (i>0){
            return Result.builder().code("200").msg("成功").data(detail.getId()).build();
        }else {
            return Result.builder().code("500").msg("插入失败").data(null).build();
        }
   }
   @PostMapping("updateDetail")
   public Result updateDetail(@RequestBody Detail detail){
       if (detail==null){
           return Result.builder().code("500").msg("参数为空").data(null).build();
       }
       LambdaQueryWrapper<Detail> wrapper=new LambdaQueryWrapper<>();
       wrapper.eq(Detail::getId,detail.getId());
       int i = detailMapper.update(detail,wrapper);
       if (i>0){
           return Result.builder().code("200").msg("成功").data(null).build();
       }else {
           return Result.builder().code("500").msg("插入失败").data(null).build();
       }
   }
   @GetMapping("getDetailByPage")
   @RequiresPermissions("select_Detail")
   public Result getDetailByPage(String username,Integer current,Integer size,String isdelete){
       LambdaQueryWrapper<Detail> wrapper=new LambdaQueryWrapper<>();
       wrapper.like(Detail::getUserId,username);
       wrapper.eq(Detail::getIsDelete,isdelete);
       IPage<Detail> page = new Page<>(current, size);
       detailMapper.selectPage(page, wrapper);
       DetailVo userVo =  DetailVo.builder().current(current).size(size).total(page.getTotal()).detailList(page.getRecords()).
               build();
       if (page.getTotal()>0){
           return Result.builder().code("200").data(userVo).msg("查询成功!!").build();
       }
       return Result.builder().code("500").data(null).msg("查询为空!!").build();
   }
}

